package kni.hoid;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class BarcodeActivity extends Activity {
	private boolean alreadyScanned = false;
	private TextView c1;

	// this part removes duplicates in the array, sorts it, then inflates it out
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!alreadyScanned) {
			alreadyScanned = true;
			final Intent in = new Intent("com.google.zxing.client.android.SCAN");
			in.putExtra("SCAN_MODE", "ONE_D_MODE");
			startActivityForResult(in, 0);
			c1 = new TextView(this);
			setContentView(c1);
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String content = intent.getStringExtra("SCAN_RESULT");
				Intent inte = new Intent().setClass(BarcodeActivity.this,
						VufindActivity.class);
				
				Item item = new Item();
				item.setType(Item.Type.ISBN);
				item.setWhat(content);
				List<Item> searchList = Arrays.asList(item);
				SearchService vufind = new VufindSearchService();
				final List<Book> vufindResults = vufind.search(searchList);
				
				inte.putExtra("result", (Serializable)vufindResults);
				startActivity(inte);
				// redirect to vufind search
			} else {
				c1.setText("error");
				// handle error
			}
		}
	}
}
